package com.my.xinyidaixi.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.my.xinyidaixi.DataBase.Bean.Address;
import com.my.xinyidaixi.DataBase.Bean.Schoolinfo;
import com.my.xinyidaixi.DataBase.Bean.User;
import com.my.xinyidaixi.DataBase.Model.Over_UserModer;
import com.my.xinyidaixi.DataBase.Model.UserModel;
import com.my.xinyidaixi.DataBase.View.UserView;
import com.my.xinyidaixi.DataBase.presenter.Userpresenter;
import com.my.xinyidaixi.HttpVolley.Volley_core;
import com.my.xinyidaixi.R;
import com.my.xinyidaixi.Util.wheelview_util.OptionPicker;
import com.my.xinyidaixi.Util.wheelview_util.Wheel_View;

import java.util.ArrayList;

/**
 * Created by zw on 2017/8/9.
 */
public class tianjia_activity extends Activity implements UserView{

    private ImageView fanhui;
    private RelativeLayout tianjiaschool;
    private TextView tianjiaschooltext,saveButton,tianjiaButton;
    private EditText address,name,telphone;
    private RadioGroup sexGrop;
    private RadioButton radioButton,female,male;
    private String man="男";
    private long id;

    private Volley_core volley=new Volley_core();

    private Userpresenter presenter=new Userpresenter(tianjia_activity.this);

    private UserModel model=new Over_UserModer();
    private Address item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tianjia_layout);
        initdata();
    }

    public void initdata()
    {
        fanhui = (ImageView) findViewById(R.id.youhuijuan_fanhui);
        fanhui.setOnClickListener(new Button());
        tianjiaschool = (RelativeLayout) findViewById(R.id.tianjia_school_layout);
        tianjiaschool.setOnClickListener(new Button());
        tianjiaschooltext = (TextView) findViewById(R.id.tianjia_school_text);
        address = (EditText) findViewById(R.id.tianjia_address);
        name = (EditText) findViewById(R.id.tianjia_name);
        sexGrop = (RadioGroup) findViewById(R.id.tianjia_RadioGroup);
        female = (RadioButton) findViewById(R.id.tianjia_female);
        male = (RadioButton) findViewById(R.id.tianjia_male);
        sexGrop.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                radioButton = (RadioButton) findViewById(checkedId);
            }
        });

        telphone = (EditText) findViewById(R.id.tianjia_telphone);
        saveButton = (TextView) findViewById(R.id.tianjia_save);
        saveButton.setOnClickListener(new Button());

        tianjiaButton = (TextView) findViewById(R.id.tianjia_tianjia);
        tianjiaButton.setOnClickListener(new Button());

        //实现编辑功能
        Intent intent=getIntent();
        item = (Address) intent.getSerializableExtra("address");
        if(item !=null)
        {
            saveButton.setVisibility(View.VISIBLE);
            tianjiaButton.setVisibility(View.GONE);
            tianjiaButton.setEnabled(false);
            name.setText(item.getName());
            if(man.equals(item.getSex())|| item.getSex()==null)
            {
                male.setChecked(true);
            }
            else
            {
                female.setChecked(true);
            }
            telphone.setText(item.getDizhi_telphonenum());
            address.setText(item.getAddress());
            tianjiaschooltext.setText(item.getSchool());
            id= item.getId();
        }
        else
        {
            saveButton.setEnabled(false);
        }
        //实现编辑功能

    }

    public  class  Button implements View.OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.youhuijuan_fanhui:
                    tianjia_activity.this.finish();
                    overridePendingTransition(0,R.anim.ani_left_sign_out);
                    break;
                case R.id.tianjia_school_layout:
                    onConstellationPicker();
                    break;
                case R.id.tianjia_tianjia:
                    if(TextUtils.isEmpty(address.getText().toString()))
                    {
                        Toast.makeText(tianjia_activity.this,"详细地址不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }

                    if(TextUtils.isEmpty(name.getText().toString()))
                    {
                        Toast.makeText(tianjia_activity.this,"姓名不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (TextUtils.isEmpty(telphone.getText().toString()))
                    {
                        Toast.makeText(tianjia_activity.this,"电话不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(TextUtils.isEmpty(tianjiaschooltext.getText().toString()))
                    {
                        Toast.makeText(tianjia_activity.this,"学校不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(TextUtils.isEmpty(radioButton.getText().toString()))
                    {
                        Toast.makeText(tianjia_activity.this,"性别不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    presenter.add_address(tianjia_activity.this); //添加到数据库中



                    tianjia_activity.this.setResult(1);
                    overridePendingTransition(0,R.anim.ani_left_sign_out);
                    tianjia_activity.this.finish();
                    break;


                case R.id.tianjia_save:
                   if(TextUtils.isEmpty(address.getText().toString()))
                   {
                       Toast.makeText(tianjia_activity.this,"详细地址不能为空",Toast.LENGTH_SHORT).show();
                       return;
                   }

                    if(TextUtils.isEmpty(name.getText().toString()))
                    {
                        Toast.makeText(tianjia_activity.this,"姓名不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (TextUtils.isEmpty(telphone.getText().toString()))
                    {
                        Toast.makeText(tianjia_activity.this,"电话不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(TextUtils.isEmpty(tianjiaschooltext.getText().toString()))
                    {
                        Toast.makeText(tianjia_activity.this,"学校不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(TextUtils.isEmpty(radioButton.getText().toString()))
                    {
                        Toast.makeText(tianjia_activity.this,"性别不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    item.setSex(radioButton.getText().toString().trim());
                    item.setSchool(tianjiaschooltext.getText().toString().trim());
                    item.setName(name.getText().toString().trim());
                    item.setDizhi_telphonenum(telphone.getText().toString().trim());
                    item.setAddress(address.getText().toString().trim());
                    model.update_address(item,id);  //添加到数据库中
                    tianjia_activity.this.setResult(2);
                    overridePendingTransition(0,R.anim.ani_left_sign_out);
                    tianjia_activity.this.finish();
                    break;
            }
        }
    }

    //添加学校
    public void onConstellationPicker()
    {
       /* Volley_core voller=new Volley_core();
        voller.Schoolinfo(true);*/
        String [] array;
        ArrayList<Schoolinfo> schoolinfos=new ArrayList<>();
        schoolinfos=model.get_school_address();
        array=new String[schoolinfos.size()];
        for (int i=0;i<schoolinfos.size();i++)
        {
            array[i]=schoolinfos.get(i).getSchool_name()+"("+schoolinfos.get(i).getSchool_address()+")";
        }
        //ToastUtil.shortToast(tianjia_activity.this,"大小为"+schoolinfos.size());

                OptionPicker  picker = new OptionPicker(tianjia_activity.this,array);
                picker.setCycleDisable(false);//不禁用循环
                picker.setTopBackgroundColor(0xFFFFFFFF);
                picker.setTopHeight(50);
                picker.setTopLineColor(0xFF00DBF5);
                picker.setTopLineHeight(1);
                picker.setTitleText("请选择");
                picker.setTitleTextColor(0xFF000000);//标题字体颜色
                picker.setTitleTextSize(13);
                picker.setCancelText("取消");
                picker.setCancelTextColor(0xFF000000);//取消字体颜色
                picker.setCancelTextSize(13);
                picker.setSubmitText("确定");
                picker.setSubmitTextColor(0xFF000000);//确定字体颜色
                picker.setSubmitTextSize(13);
                picker.setTextColor(0xFF000000, 0xFF000000); //渐变色
                Wheel_View.DividerConfig config = new Wheel_View.DividerConfig();
                config.setColor(0xFF00DBF5);//线颜色
                config.setAlpha(140);//线透明度
                config.setRatio((float) (1.0 / 8.0));//线比率
                picker.setDividerConfig(config);
                picker.setBackgroundColor(0xFFFFFFFF);
                //picker.setSelectedItem(isChinese ? "处女座" : "Virgo");
                picker.setSelectedIndex(7);
                picker.setCanceledOnTouchOutside(true);
                picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
                    @Override
                    public void onOptionPicked(int index, String item) {
                        tianjiaschooltext.setText(item);
                        //Toast.makeText(tianjia_activity.this, "index=" + index + ", item=" + item, Toast.LENGTH_SHORT).show();
                    }
                });
                picker.show();
            }

    //添加学校



    @Override
    protected void onPause()
    {
        super.onPause();
        tianjia_activity.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
    }

    @Override
    public User gettUserinf() {
        return null;
    }

    @Override
    public void setUserinf(User user) {
    }
    @Override
    public Address get_address() {
        Address item=new Address();
        item.setSex(radioButton.getText().toString().trim());
        item.setSchool(tianjiaschooltext.getText().toString().trim());
        item.setName(name.getText().toString().trim());
        item.setDizhi_telphonenum(telphone.getText().toString().trim());
        item.setAddress(address.getText().toString().trim());
        return item;
    }

    @Override
    public void setUseraddress(Address address) {

    }
}
